package com.itheima.web.controller.store.company;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Company;
import com.itheima.service.store.CompanyService;
import com.itheima.service.store.impl.CompanyServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// uri:/store/company?operation=list
@WebServlet("/store/company")
public class CompanyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if ("list".equals(operation)) {
            list(request, response);
            //todo 为什么要toAdd 再add  但delete不需要    WEB-INF中的文件是受保护的  所以需要跳转
        }else if ("toAdd".equals(operation)){
            toAdd(request, response);
        }else if ("save".equals(operation)){
            save(request, response);
        }else if ("toEdit".equals(operation)){
            toEdit(request, response);
        }else if ("edit".equals(operation)){
            edit(request, response);
        }else if ("delete".equals(operation)){
            delete(request, response);
        }
        /*String operation = request.getParameter("operation");
        if("list".equals(operation)){
            CompanyService companyService = new CompanyServiceImpl();
            PageInfo all = companyService.findAll(1, 100);
            //将数据保存到指定的位置
            request.setAttribute("page",all);
            //跳转页面
             request.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(request,response);
        }else if("list".equals(operation)){

        }else if("list".equals(operation)){

        }else if("list".equals(operation)){

        }else if("list".equals(operation)){

        }else if("list".equals(operation)){

        }*/
    }

       protected CompanyServiceImpl companyService   = new CompanyServiceImpl();
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int page = 1;
        int size = 5;

        if (StringUtils.isNotBlank(request.getParameter("page"))) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))) {
            size = Integer.parseInt(request.getParameter("size"));
        }

        PageInfo all = companyService.findAll(page, size);
//      todo      将数据存到指定的位置   这一步干了什么  为什么要设置  具体干什么
        request.setAttribute("page", all);
//        todo    转发  忘记 写完需要去看
        request.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(request, response);
    }

    protected void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/store/company/add.jsp").forward(request,response);
    }
    protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //todo 这行的参数是什么意思
        Company company = BeanUtil.fillBean(request, Company.class, "yyyy-MM-dd");
        //调用service中的方法
        companyService.save(company);
        //请求转发
        //request.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(request, response);
        //response.sendRedirect(request.getContextPath()+"/store/company?operation=list");
        list(request, response);

    }
    protected void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取原先的数据
        // 通过id进行查找
        String id = request.getParameter("id");
        Company company = companyService.findById(id);
        //传递给页面
        request.setAttribute("company",company);
        request.getRequestDispatcher("/WEB-INF/pages/store/company/update.jsp").forward(request,response);
    }
    protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Company company = BeanUtil.fillBean(request, Company.class, "yyyy-MM-dd");

        //调用service中的方法
        companyService.update(company);

        list(request, response);
    }
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // todo 删除功能不能删除第一行为什么
       String id = request.getParameter("id");
        //Company company = BeanUtil.fillBean(request, Company.class, "yyyy-MM-dd");
        //Company company = BeanUtil.fillBean(request, Company.class);

       // companyService.delete(company);
        companyService.delete(id);
        list(request,response);
    }

        @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
